package com.pig4cloud.mall.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.mall.product.entity.Attr;
import com.pig4cloud.mall.product.service.AttrService;
import com.pig4cloud.pig.common.core.util.PageResult;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 商品属性(规格参数)
 *
 * @author fangyu
 * @date 2020-11-25 15:05:14
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/attr")
@Api(value = "attr", tags = "商品属性管理")
public class AttrController {

	private final AttrService attrService;

	/**
	 * 通过spuId获取spu规格
	 *
	 * @param spuId spuId
	 * @return R
	 */
	@ApiOperation(value = "通过spuId查询", notes = "通过spuId查询")
	@GetMapping("/base/spu/{spuId}")
//	@PreAuthorize("@pms.hasPermission('product_attr_get')")
	public R getSpuSpecification(@RequestParam Map<String, Object> params, @PathVariable("spuId") Long spuId) {
		PageResult pageResult = attrService.queryPage(params, spuId);
		return R.ok(pageResult);
	}


	/**
	 * 通过catId获取对应类型的spu规格分页
	 *
	 * @param params 查询条件
	 * @param attrType 规格属性类型
	 * @param catId catId 分类id
	 * @return R
	 */
	@ApiOperation(value = "通过catId查询", notes = "通过catId查询")
	@GetMapping("/{attrType}/list/{catId}")
//	@PreAuthorize("@pms.hasPermission('product_attr_get')")
	public R list(@RequestParam Map<String, Object> params, @PathVariable("attrType") String attrType, @PathVariable("catId") Long catId) {
		PageResult pageResult = attrService.queryPageByCid(params, attrType, catId);
		return R.ok(pageResult);
	}


	/**
	 * 通过catId获取对应类型的所有spu规格
	 *
	 * @param attrType 属性类型
	 * @param catId catId 分类id
	 * @return R
	 */
	@ApiOperation(value = "通过catId查询", notes = "通过catId查询")
	@GetMapping("/{attrType}/all/{catId}")
//	@PreAuthorize("@pms.hasPermission('product_attr_get')")
	public R base(@PathVariable("attrType") String attrType, @PathVariable("catId") Long catId) {
		if ("base".equals(attrType)) {
			return attrService.queryBaseAttrByCid(catId);
		} else if ("sale".equals(attrType)) {
			return attrService.querySaleAttrByCid(catId);
		}
		return R.failed("不存在此属性");
	}


	/**
	 * 分页查询
	 *
	 * @param page 分页对象
	 * @param attr 商品属性
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('product_attr_get')")
	public R getAttrPage(Page page, Attr attr) {
		return R.ok(attrService.page(page, Wrappers.query(attr)));
	}


	/**
	 * 通过id查询商品属性
	 *
	 * @param attrId id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{attrId}")
	@PreAuthorize("@pms.hasPermission('product_attr_get')")
	public R getById(@PathVariable("attrId") Long attrId) {
		return R.ok(attrService.getById(attrId));
	}

	/**
	 * 新增商品属性
	 *
	 * @param attr 商品属性
	 * @return R
	 */
	@ApiOperation(value = "新增商品属性", notes = "新增商品属性")
	@SysLog("新增商品属性")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('product_attr_add')")
	public R save(@RequestBody Attr attr) {
		return R.ok(attrService.save(attr));
	}

	/**
	 * 修改商品属性
	 *
	 * @param attr 商品属性
	 * @return R
	 */
	@ApiOperation(value = "修改商品属性", notes = "修改商品属性")
	@SysLog("修改商品属性")
	@PutMapping
//	@PreAuthorize("@pms.hasPermission('product_attr_edit')")
	public R updateById(@RequestBody Attr attr) {
		return R.ok(attrService.updateById(attr));
	}

	/**
	 * 通过id删除商品属性
	 *
	 * @param attrId id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除商品属性", notes = "通过id删除商品属性")
	@SysLog("通过id删除商品属性")
	@DeleteMapping("/{attrId}")
	@PreAuthorize("@pms.hasPermission('product_attr_del')")
	public R removeById(@PathVariable Long attrId) {
		return R.ok(attrService.removeById(attrId));
	}

}
